package com.csi.service.impl;

import com.csi.domain.HotCity;
import com.csi.domain.Result;
import com.csi.mapper.HotCityMapper;
import com.csi.service.HotCityService;
import com.csi.utils.ReturnResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class HotCityServiceImpl implements HotCityService {
    @Autowired
    private HotCityMapper hotCityMapper;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    private static final String KEY_PREFIX = "hotCity:";

    /**
     * 显示热门城市
     * @return
     */
    @Override
    public List<HotCity> showHotCity() {
        List<HotCity> hotCities = (List<HotCity>)redisTemplate.opsForValue().get(KEY_PREFIX);

        if (hotCities == null){
            hotCities = hotCityMapper.showHotCity();
            if (hotCities != null && hotCities.size()!=0){
                redisTemplate.opsForValue().set(KEY_PREFIX,hotCities);
                System.out.println("=====================热门城市未放入缓存");
                return hotCities;
            }
        }else {
            System.out.println("=====================热门城市已在缓存");
            hotCities.forEach(hotCity -> System.out.println(hotCity.getCityName()));
            return hotCities;
        }
        return hotCities;
    }
}
